from tkinter.messagebox import showinfo
from threading import Thread
from time import sleep
import sys
from tkinter import Tk
from tkinter import Label
from tkinter import Entry
from tkinter import Button
from tkinter import Frame
from tkinter import LEFT
from tkinter import END
from tkinter import Toplevel
from tkinter import Menu
from tkinter import IntVar
from about import About
from remote import Remote
from windowsinit import *
import sys
import os
from text import INFO

class Pts_mini(Tk):
    """客户端"""
    def __init__(self):
        super(Pts_mini, self).__init__()
        self.language = IntVar()  # 创建一个变量
        self.language.set(1)  # 设置初值(中文)
        self.window_init(self.language.get())
        self.menubar_init()
        self.remote()
        if os.path.exists(resource_path(os.path.join("res", "synctech.ico"))):
            self.iconbitmap(resource_path(os.path.join("res", "synctech.ico")))
    def window_init(self,language):
        """窗口属性初始化"""
        WindowInit(self,language)

    def aboutfunc(self):
        top.attributes("-disabled", 1)
        about = About(top,self.language)
        self.wait_window(about)  # 这一句很重要！！！
        return
    def change_language(self):
        print(self.language.get())
        self.c_remote.strvar_localIP.set(INFO['Local IP:'][self.language.get()])
        self.c_remote.strvar_deviceIP.set(INFO['Device IP:'][self.language.get()])
        self.c_remote.strvar_Progress.set(INFO['Progress:'][self.language.get()])
        self.c_remote.button_per.set(INFO['Update'][self.language.get()])
        self.c_remote.strvar_chooseFile.set(INFO['Choose File'][self.language.get()])
        self.c_remote.ip_config_label.set(INFO['Serial Port'][self.language.get()])
        self.c_remote.ip_config_but.set(INFO['update port'][self.language.get()])
        self.c_remote.ip_config_set.set(INFO['Set ip'][self.language.get()])
        self.c_remote.ip_config_but_r.set(INFO['Read IP'][self.language.get()])
        self.c_remote.ip_config_but_w.set(INFO['Write IP'][self.language.get()])
        # self.window_init(self.language.get())
        self.title(INFO['Title'][self.language.get()] + ' V2.01')
        self.menubar_init()


    def menubar_init(self):
        menubar = Menu(self)
        aboutmenu = Menu(menubar,tearoff=0)
        aboutmenu.add_command(label=INFO['About'][self.language.get()], command=self.aboutfunc)
        language_menu = Menu(menubar, tearoff=0)
        language_menu.add_radiobutton(label='中文', variable=self.language, value=0, command=self.change_language)
        language_menu.add_radiobutton(label='English', variable=self.language, value=1, command=self.change_language)
        menubar.add_cascade(label=INFO['Help'][self.language.get()],menu=aboutmenu)
        menubar.add_cascade(label=INFO['Language'][self.language.get()], menu=language_menu)

        self.config(menu=menubar)

    def remote(self):
        self.c_remote = Remote(self,self.language)


    def on_window_closed(self, command):
        """窗口关闭事件处理方法"""
        self.protocol('WM_DELETE_WINDOW', command)


def resource_path(relative_path):
    # if getattr(sys, 'frozen', False): #是否Bundle Resource
    #     base_path = sys._MEIPASS
    # else:
    base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

if __name__ == '__main__':

    top = Pts_mini()
    top.mainloop()
